local js_biaozhao = fk.CreateSkill {
  name = "js__biaozhao",
}


js_biaozhao:addEffect(fk.EventPhaseStart , {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start and #player.room.alive_players > 2
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player, false), Util.IdMapper)
    local tos = room:askForChoosePlayers(player, targets, 2, 2, "#js__biaozhao-choose", self.name, true)
    if #tos == 2 then
      event:setCostData(self,tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target1, target2 = room:getPlayerById(event:getCostData(self)[1]), room:getPlayerById(event:getCostData(self)[2])
    room:addTableMark(target1, "@@js__biaozhao1", player.id)
    room:addTableMark(target2, "@@js__biaozhao2", player.id)
  end,
})
js_biaozhao:addEffect(fk.TurnStart , {
  can_refresh = function(self, event, target, player, data)
    return target == player and table.find(player.room.alive_players, function(p)
      return table.contains(p:getTableMark("@@js__biaozhao1"), player.id) or table.contains(p:getTableMark("@@js__biaozhao2"), player.id)
    end)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:getMark("@@js__biaozhao1") ~= 0 then
        room:removeTableMark(p, "@@js__biaozhao1", player.id)
      end
      if p:getMark("@@js__biaozhao2") ~= 0 then
        room:removeTableMark(p, "@@js__biaozhao2", player.id)
      end
    end
  end,
})
js_biaozhao:addEffect(fk.Death , {
  can_refresh = function(self, event, target, player, data)
    return target == player and table.find(player.room.alive_players, function(p)
      return table.contains(p:getTableMark("@@js__biaozhao1"), player.id) or table.contains(p:getTableMark("@@js__biaozhao2"), player.id)
    end)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:getMark("@@js__biaozhao1") ~= 0 then
        room:removeTableMark(p, "@@js__biaozhao1", player.id)
      end
      if p:getMark("@@js__biaozhao2") ~= 0 then
        room:removeTableMark(p, "@@js__biaozhao2", player.id)
      end
    end
  end,
})

js_biaozhao:addEffect("targetmod" , {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and player:getMark("@@js__biaozhao1") ~= 0 and scope == Player.HistoryPhase and to and to:getMark("@@js__biaozhao2") ~= 0 and
      table.find(to:getMark("@@js__biaozhao2"), function(id) return table.contains(player:getMark("@@js__biaozhao1"), id) end)
  end,
  bypass_distances = function(self, player, skill, card, to)
    return card and player:getMark("@@js__biaozhao1") ~= 0 and to and to:getMark("@@js__biaozhao2") ~= 0 and
      table.find(to:getMark("@@js__biaozhao2"), function(id) return table.contains(player:getMark("@@js__biaozhao1"), id) end)
  end,
})

js_biaozhao:addEffect(fk.DamageCaused , {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@js__biaozhao2") ~= 0 and data.card and
      table.contains(player:getMark("@@js__biaozhao2"), data.to.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.to:broadcastSkillInvoke("js__biaozhao")
    room:notifySkillInvoked(data.to, "js__biaozhao", "negative")
    data.damage = data.damage + 1
  end,
})

return js_biaozhao